// 哈希桶的简单实现
// key-value 模型
public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;


        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }


    private Node[] array;
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小

    public int put(int key, int value) {
        int index = key % array.length;
        Node cur = array[index];
        while (cur != null) {
            if (cur.key == key) {
                return key;
            }
            cur = cur.next;
        }
        Node node = new Node(key, value);
        node.next = array[index];
        array[index] = node;
        size++;
       double loadFactor=loadFactor();
        if(loadFactor>LOAD_FACTOR){
            resize();
        }

        return -1;

}
 
 
    private void resize() {
        //扩容意味着重新哈希
        Node[] newArray=new Node[2*array.length];
        for(int i=0;i<array.length;i++){
            Node cur=array[i];
            while(cur!=null){
                Node curNext=cur.next;//记录一下，因为是在重新哈希，所以下一个结点会出现找不到的情况
                int index=cur.key%newArray.length;
                cur.next=newArray[i];
                newArray[i]=cur;
                cur=curNext;

            }
        }
    }
        // write code here
    //
    //
    private double loadFactor() {
              return size * 1.0 / array.length;
          }
          public int get(int key){
        int index=key%array.length;
        Node cur=array[index];
        while(cur!=null){
            if(cur.key==key){
                return cur.key;
            }
            cur=cur.next;
        }

        return -1;
          }
    }
 
 

 